#include "shortcutui.h"


ShortcutUi::ShortcutUi()
{
    setAttribute(Qt::WA_DeleteOnClose);
    uiLayout     = new QVBoxLayout(this);
    systemLayout = new QVBoxLayout(this);
    customLayout = new QVBoxLayout(this);
    uiLayout->addLayout(systemLayout);
    uiLayout->addLayout(customLayout);
    uiLayout->addStretch();
    uiLayout->setSpacing(40);
    uiLayout->setMargin(0);
    initUi();
}

ShortcutUi::~ShortcutUi()
{

}

void ShortcutUi::initUi()
{
    initSystem();
    initCustom();
}

void ShortcutUi::initSystem()
{
    systemTitle         = new TitleLabel(this);
    systemShortcutGroup = new SettingGroup(this);
    //~ contents_path /Shortcut/System Shortcut
    systemTitle->setText(tr("System Shortcut"));
    systemLayout->addWidget(systemTitle);
    systemLayout->setSpacing(8);
    systemLayout->addWidget(systemShortcutGroup);
}

void ShortcutUi::initCustom()
{
    customTitle         = new TitleLabel(this);
    customShortcutGroup = new SettingGroup(this);
    addButton           = new AddButton(this);
    QHBoxLayout *layout = new QHBoxLayout(this);
    //~ contents_path /Shortcut/Customize Shortcut
    customTitle->setText(tr("Customize Shortcut"));
    customLayout->setSpacing(0);
    customLayout->addWidget(customTitle);
    customLayout->addSpacing(8);
    customLayout->addWidget(customShortcutGroup);
    customLayout->addSpacing(1);
    customLayout->addLayout(layout);
    layout->addWidget(addButton);

    addButton->setRadiusType(UkccFrame::BorderRadiusStyle::Around);
    customShortcutGroup->setOneRadiusStyle(UkccFrame::BorderRadiusStyle::Top);
    customShortcutGroup->setLastRadiusStyle(UkccFrame::BorderRadiusStyle::None);

    connect(addButton, &AddButton::clicked, this, &ShortcutUi::addButtonClicked);

}

void ShortcutUi::addSystemShortcut(const QString &name, const QString &shortcut)
{
    UkccFrame   *shortCutFrame = new UkccFrame(this);
    FixLabel    *nameLabel     = new FixLabel(shortCutFrame);
    FixLabel    *bindingLabel  = new FixLabel(shortCutFrame);
    QHBoxLayout *gHorLayout    = new QHBoxLayout(shortCutFrame);

    shortCutFrame->setFixedHeight(52);
    gHorLayout->setSpacing(140);
    gHorLayout->setContentsMargins(16, 0, 16, 0);

    nameLabel->setText(name);
    bindingLabel->setText(shortcut);
    bindingLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    QSizePolicy policy;
    policy = nameLabel->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Ignored);
    nameLabel->setSizePolicy(policy);

    policy = bindingLabel->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Ignored);
    bindingLabel->setSizePolicy(policy);
    gHorLayout->addWidget(nameLabel, 1);
    gHorLayout->addWidget(bindingLabel, 1);
    systemShortcutGroup->addWidget(shortCutFrame);
}

void ShortcutUi::addCustomShortcut(KeyEntry keyEntry, KeyEntryList *systemEntryList, KeyEntryList *customEntryList)
{
    UkccFrame           *frame            = new UkccFrame(this, UkccFrame::BorderRadiusStyle::Around, true);
    QHBoxLayout         *layout           = new QHBoxLayout(frame);
    QHBoxLayout         *lineEditLayout   = new QHBoxLayout;
    DoubleClickLineEdit *nameLineEdit     = new DoubleClickLineEdit(customEntryList, frame);
    DoubleClickShortCut *bingdingLineEdit = new DoubleClickShortCut(systemEntryList, customEntryList);
    ClickFixLabel       *nameLabel        = new ClickFixLabel(frame);
    ClickFixLabel       *bingdingLabel    = new ClickFixLabel(frame);
    KeyEntry            *mKeyEntry        = new KeyEntry;

    mKeyEntry->gsSchema   = keyEntry.gsSchema;
    mKeyEntry->keyStr     = keyEntry.keyStr;
    mKeyEntry->valueStr   = keyEntry.valueStr;
    mKeyEntry->descStr    = keyEntry.descStr;
    mKeyEntry->gsPath     = keyEntry.gsPath;
    mKeyEntry->nameStr    = keyEntry.nameStr;
    mKeyEntry->bindingStr = keyEntry.bindingStr;
    mKeyEntry->actionStr  = keyEntry.actionStr;

    customShortcutGroup->addWidget(frame);
    addButton->setRadiusType(UkccFrame::BorderRadiusStyle::Bottom);

    // 大小写字母数字中文
    QRegExp rx("[a-zA-Z0-9\u4e00-\u9fa5]+");
    QRegExpValidator *regValidator = new QRegExpValidator(rx);
    // 输入限制
    nameLineEdit->setValidator(regValidator);
    nameLineEdit->setFixedHeight(36);
    bingdingLineEdit->setFixedHeight(36);

    bingdingLineEdit->setAlignment(Qt::AlignRight);
    bingdingLabel->setAlignment(Qt::AlignRight | Qt::AlignCenter);

    QToolButton *btn = new QToolButton(frame);
    btn->setAutoRaise(true);
    btn->setPopupMode(QToolButton::InstantPopup);
    QMenu *menu = new QMenu(btn);
    btn->setMenu(menu);
    QAction *edit = new QAction(btn);
    QAction *del = new QAction(btn);
    edit->setText(tr("Edit"));
    del->setText(tr("Delete"));
    menu->addAction(edit);
    menu->addAction(del);

    connect(del, &QAction::triggered, this, [=](){
        customShortcutGroup->removeWidget(frame);
        if (customShortcutGroup->showItemsCount() == 0) {
            addButton->setRadiusType(UkccFrame::BorderRadiusStyle::Around);
        }
        frame->deleteLater();
        Q_EMIT deleteShortcut(mKeyEntry->gsPath);
        for (int i = 0; i < customEntryList->size(); ++i) {
            if (customEntryList->at(i).nameStr == mKeyEntry->nameStr) {
                customEntryList->removeAt(i);
                break;
            }
        }
        delete mKeyEntry;
    });

    connect(edit, &QAction::triggered, this, [=](){
        AddShortcutDialog *addDialog = new AddShortcutDialog(systemEntryList, customEntryList, this);
        addDialog->setAttribute(Qt::WA_DeleteOnClose);
        addDialog->setSourceEnable(false);
        addDialog->setWindowTitle(QObject::tr("Edit Shortcut"));

        addDialog->setExecText(mKeyEntry->actionStr);
        addDialog->setNameText(mKeyEntry->nameStr);
        addDialog->setKeyText(mKeyEntry->bindingStr);
        addDialog->setKeyIsAvailable(3);
        connect(addDialog, &AddShortcutDialog::shortcutInfoSignal, this,
                [=](QString path, QString name, QString exec, QString key){
            Q_UNUSED(path)
            Q_EMIT toCreateShortcut(mKeyEntry->gsPath, name, exec, key, false);
            nameLineEdit->setToolTip(name);
            nameLineEdit->blockSignals(true);
            nameLineEdit->setText(name);
            nameLineEdit->blockSignals(false);
            bingdingLineEdit->blockSignals(true);
            QString mKey = getShowShortcutString(addDialog->keyToLib(key));
            if (mKey.contains("Meta")) {
                mKey.replace("Meta", "Win");
            }
            bingdingLineEdit->setText(mKey);
            bingdingLineEdit->blockSignals(false);
            nameLabel->setText(name);
            bingdingLabel->setText(bingdingLineEdit->text());
        });
        addDialog->exec();
    });

    btn->setIcon(QIcon::fromTheme("view-more-horizontal-symbolic"));
    btn->setProperty("useButtonPalette", true);
    btn->setFixedSize(36, 36);

    layout->setContentsMargins(8, 0, 16, 0);
    layout->setSpacing(24);
    layout->addLayout(lineEditLayout);
    layout->addWidget(btn);

    lineEditLayout->setMargin(0);
    lineEditLayout->setSpacing(140);

    QSizePolicy policy;
    policy = nameLineEdit->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Ignored);
    nameLineEdit->setSizePolicy(policy);

    policy = nameLabel->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Ignored);
    nameLabel->setSizePolicy(policy);

    policy = bingdingLineEdit->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Ignored);
    bingdingLineEdit->setSizePolicy(policy);

    policy = bingdingLabel->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Ignored);
    bingdingLabel->setSizePolicy(policy);

    lineEditLayout->addWidget(nameLineEdit, 1);
    lineEditLayout->addWidget(nameLabel, 1);
    lineEditLayout->addWidget(bingdingLineEdit, 1);
    lineEditLayout->addWidget(bingdingLabel, 1);
    nameLineEdit->setVisible(false);
    bingdingLineEdit->setVisible(false);

    nameLineEdit->blockSignals(true);
    nameLineEdit->setText(mKeyEntry->nameStr);
    nameLineEdit->blockSignals(false);
    bingdingLineEdit->blockSignals(true);
    bingdingLineEdit->setText(getShowShortcutString(mKeyEntry->bindingStr));
    bingdingLineEdit->blockSignals(false);

    nameLabel->setText(mKeyEntry->nameStr);
    bingdingLabel->setText(getShowShortcutString(mKeyEntry->bindingStr));


    connect(nameLabel,&ClickFixLabel::doubleClicked,this,[=](){
        nameLabel->hide();
        nameLineEdit->show();
        nameLineEdit->setFocus();
    });

    connect(bingdingLabel,&ClickFixLabel::doubleClicked,this,[=](){
        bingdingLabel->hide();
        bingdingLineEdit->show();
        bingdingLineEdit->setFocus();
    });

    connect(nameLineEdit,&DoubleClickLineEdit::focusOut,this,[=](){
        nameLabel->show();
        nameLineEdit->hide();
    });

    connect(bingdingLineEdit,&DoubleClickShortCut::focusOut,this,[=](){
        bingdingLabel->show();
        bingdingLineEdit->hide();
    });

    connect(nameLineEdit, &DoubleClickLineEdit::strChanged, this, [=](){
        Q_EMIT toCreateShortcut(mKeyEntry->gsPath, nameLineEdit->text(), mKeyEntry->actionStr, mKeyEntry->bindingStr, false, false); //只修改
        nameLabel->setText(nameLineEdit->text());
        mKeyEntry->nameStr = nameLineEdit->text();
    });

    connect(bingdingLineEdit, &DoubleClickShortCut::shortcutChanged, this, [=](){
        Q_EMIT toCreateShortcut(mKeyEntry->gsPath, mKeyEntry->nameStr, mKeyEntry->actionStr, bingdingLineEdit->keySequence().toString(), false, true); //只修改
        bingdingLineEdit->blockSignals(true);
        bingdingLineEdit->setText(getShowShortcutString(bingdingLineEdit->text()));
        bingdingLineEdit->blockSignals(false);
        bingdingLabel->setText(bingdingLineEdit->text());
        for (int i = 0; i < customEntryList->size(); i++) {
            if (customEntryList->at(i).nameStr == mKeyEntry->nameStr) {
                mKeyEntry->keyStr = customEntryList->at(i).keyStr;
                break;
            }
        }
    });

    return;
}

QString ShortcutUi::getShowShortcutString(QString src) {
    src.replace("<","");
    src.replace(">","   ");
    src.replace(" or ",tr(" or "));
    QStringList temp_list = src.split(" ");
    QString str;
    for(qint32 i = 0; i < temp_list.count(); i++) {
        str += temp_list.at(i)
                    .left(1).toUpper() +
                    temp_list.at(i)
                    .mid(1, temp_list.at(i).length() - 1).toLower();
        str += " ";
    }
    str.replace("Or","or");
    return  str;
}

void ShortcutUi::clearCustomShorcutFrame()
{
    customShortcutGroup->removeAndDeleteAllWidget();
}
